//
//  MainViewController.swift
//  微博项目
//
//  Created by Mac on 2017/5/13.
//  Copyright © 2017年 Mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    fileprivate lazy var composeBtn:UIButton = UIButton.init(imageName:"tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpCompseBtn()
    }
    // 文件内私有的方法
    fileprivate func addChildViewController(childVC:String, title:String, imageName:String) ->  ()
    {
        // 获取命名空间
        guard let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
           print("没有获取到命名空间")
           return
        }
        
        guard let childvcClass = NSClassFromString(nameSpace + "." + childVC) else {
            print("没有获取到对应的class ")
            return
        }
        
        // 将获取到的class 转换成控制器的类型
        guard let childVcType = childvcClass as? UIViewController.Type else {
            print("没有获取类型")
            return
        }
        
        let childVC = childVcType.init()
        //创建对应的控制器
        childVC.title = title
        childVC.tabBarItem.image = UIImage.init(named:imageName)
        childVC.tabBarItem.selectedImage = UIImage.init(named: imageName + "_highlighted")
        //包装导航控制器
        let childNav = UINavigationController.init(rootViewController:childVC)
        addChildViewController(childNav)
    }
    
}

extension MainViewController{
    fileprivate func setUpCompseBtn(){
        // 添加发布按钮
        tabBar.addSubview(composeBtn)
        // 位置
        composeBtn.center =  CGPoint.init(x: tabBar.center.x, y:tabBar.bounds.size.height * 0.5)
        composeBtn.addTarget(self, action:#selector(self.composeBtnClick), for:.touchUpInside)
    }
}

// 事件监听
extension MainViewController{
    @objc fileprivate func composeBtnClick(){
      print("composebtnclick")
    }
}
